package life;

public class Food
{

	Coordinate position;
	double energy;
	double age;
	double foodLifeSpan;
	
	public Food(Coordinate position, double energy)
	{
		this.position = position;
		this.energy = energy;
	}
	
	public Food(World w,int foodLifeSpan)
	{
		this.position= new Coordinate(Coordinate.RANDOM, w);
		this.energy=Math.floor(Math.random()*20+1);
		this.age=0;
		this.foodLifeSpan=foodLifeSpan;
	}

	public Food()
	{
	}

	public void aged()
	{
		age++;		
	}

	public boolean isStillAlive()
	{
		if (age<=foodLifeSpan)
			return true;
		double deathProbability=1.0-1.0/(age-foodLifeSpan+1); //1+1/(x+1)
		
		if(Math.random()>deathProbability)
			return true;
		return false;
	}

	@Override
	public String toString()
	{
		return "Food [position=" + position + ", energy=" + energy + ", age=" + age + "]\n";
	}
	

	public int getX(){return (int) position.x;}
	public int gety(){return (int) position.y;}

	public StringBuffer dumpToString()
	{
		StringBuffer sb = new StringBuffer("f;");
		sb.append(energy);
		sb.append(";");
		sb.append(position.x);
		sb.append(";");
		sb.append(position.y);
		sb.append(";");
		sb.append(age);
		sb.append(";");
		sb.append(foodLifeSpan);	
		
		return sb;
	}

	public Coordinate getPosition()
	{
		return position;
	}

	public void setPosition(Coordinate position)
	{
		this.position = position;
	}

	public double getEnergy()
	{
		return energy;
	}

	public void setEnergy(double energy)
	{
		this.energy = energy;
	}

	public double getAge()
	{
		return age;
	}

	public void setAge(double age)
	{
		this.age = age;
	}

	public double getFoodLifeSpan()
	{
		return foodLifeSpan;
	}

	public void setFoodLifeSpan(double foodLifeSpan)
	{
		this.foodLifeSpan = foodLifeSpan;
	}

}
